<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/jquery-1.10.1.min.js"></script>
  <link rel="stylesheet" href="../css/public.css">
  <link rel="stylesheet" href="../css/cart.css">
  <script src="../js/vue.js"></script>
  <script src="../js/axios.js"></script>
  <script src="../js/jquery-1.10.1.min.js"></script>

  <style>
    .a {
      background-color: red;
    }

    button {
      background-color: white;
    }
  </style>
</head>

<body>
  <header class="header"></header>

  <div class="content">
    <div class="page-container">
      <div class="cart-to-login" v-if="!user">
        <h4>您还没有登录</h4>
        <p>立即去 <a class="goToLogin" href="javascript:;">登入</a>或者<a class="goToRegister" href="javascript:;">注册</a>FILA会员
        </p>
      </div>
      <div class="cartMain">
        <h3 class="cart-title">
          <span>我的购物车</span>
          <i class="total-num">共 {{totalNum}} 件</i>
        </h3>
        <div class="cart-top">
          <label class="checkbox-label checkbox-all">
            <input class="checkbox-input" type="checkbox" :checked="checkedAll" @click="setCheckedAll">

            <span>全选</span>
          </label>
        </div>
        <!-- 购物车详情 -->
        <table class="cart-table" cellspacing="0" cellpadding="0">

          <tr class="goods-item" v-for="item in list" :key="item.id">
            <td class="td-check">
              <label class="checkbox-label checkbox-one">
                <input class=" check-one" type="checkbox" v-model="item.ischecked">

              </label>
            </td>
            <td class="td-img">
              <img :src="item.goodsimg" alt="">
            </td>
            <td class="td-infos">
              <div class="td-infos-top clearfix">
                <div class="fl">
                  <h5>{{item.goodsname}}</h5>
                  <p><span>颜色:乌卡拉银;尺寸:XL</span></p>
                </div>
                <div class="fr">
                  <p><span class="rmbOne">￥</span><span class="infos-price">{{item.goodsprice}}</span></p>
                  <p><span class="rmbTwo">￥</span>
                    <span class="infos-mktprice">{{(item.goodsprice*1.2).toFixed(0)}}</span>
                  </p>
                </div>
              </div>
              <div class="td-infos-bot clearfix">
                <p class="fl">
                  <span>数量：</span>
                  <button class="reduce" v-if="item.buynum>1" @click=" calc(item,-1)">-</button>
                  <button class="reduce" v-else desabled="desabled">-</button>
                  <input type="text" class="num-input" v-model="item.buynum" />
                  <button class="add" @click="calc(item,1)">+</button>
                  <span class="stock-tip">库存不足</span>
                </p>
                <p class="fr">
                  <span class="delete-one" @click="delBtn(item.id)">删除</span>
                </p>
              </div>
            </td>
          </tr>

        </table>
      </div>
      <div class="cartFoot">
        <div class="sum-main clearfix w-1280">
          <div class="sum-infos fl clearfix">
            <div class="sum-operate fl">
              <label class="checkbox-label checkbox-all">
                <input class="checkbox-input" type="checkbox" :checked="checkedAll" @click="setCheckedAll">
                <!-- <i></i> -->
                <span>全选商品</span>
              </label>
              <span class="delete-multi" @click="deleteAll">删除商品</span>
            </div>
            <div class="sum-totalprice fr">
              <span>总价</span>
              <span class="price">￥{{Number(totalPrice).toFixed(2)}}</span>
            </div>
          </div>
          <div class="sum-to-settle fl">
            <span class="btn-to-settle">结算</span>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- 底部结算 -->
  <footer class="footer"></footer>
</body>
<script>
  $(".header").load("header.html");
  $(".footer").load("footer.html")


  new Vue({
    el: ".content",
    data: {
      list: [],
      checkedAll: false,
      totalNum: "",
      totalPrice: "",
      user: "",
    },
    methods: {
      async calc(item, num) {
        item.buynum = item.buynum + num;
        await axios({
          method: "patch",
          url: "http://localhost:2345/shoppingcar/" + item.id,
          data: {
            buynum: item.buynum,
          }
        })
      },
      setCheckedAll() {
        let flag = window.event.target.checked;
        // console.log(flag);
        this.checkedAll = flag;
        this.list.forEach(item => item.ischecked = flag);
      },
      async delOne(id) {//封装单删
        await axios({
          method: "delete",
          url: "http://localhost:2345/shoppingcar/" + id,
        })
        this.list = this.list.filter(item => item.id != id);//留下所有不等于当前点击的id的item
      },
      async delBtn(id) {
        if (confirm("是否删除该商品")) {
          this.delOne(id);
        }
      },

      deleteAll() {
        // this.list = this.list.filter(item => !item.ischecked);
        let newList = this.list.filter(item => item.ischecked);
        if (newList.length) {

          if (confirm("是否删除选中的商品")) {
            newList.forEach(item => {
              if (item.ischecked) {
                let id = item.id;
                this.delOne(id);
              }
            })
          }

        } else {
          alert("您还未选择商品")
        }


      }
    },
    watch: {
      list: {
        deep: true,
        handler(newList) {
          var num = 0;
          var price = 0;
          if (newList.length) {
            this.checkedAll = this.list.every(item => item.ischecked)
            let seletList = newList.filter(item => item.ischecked);

            if (seletList.length) {
              num = seletList.map(item => item.buynum).reduce((a, b) => a + b);
              price = seletList.map(item => item.buynum * item.goodsprice).reduce((a, b) => a + b);
            }

          } else {
            this.checkedAll = false;
          }

          this.totalNum = num;
          this.totalPrice = price;
        }
      }
    },
    async created() {
      let userphone = window.localStorage.getItem("userinfo");
      userphone = JSON.parse(userphone).phone;
      this.user = userphone;
      console.log(userphone);
      if (userphone) {
        let { data: list } = await axios({
          method: "get",
          url: "http://localhost:2345/shoppingcar",
          params: {
            userphone,
          }
        })
        this.list = list.map(item => {
          item.ischecked = false;
          return item;
        })
        console.log(list);
      }

    }
  })

</script>

</html>